/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* includes (local) ----------------------------------------------------------*/
#include "k_cls.h"
/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include "x_bug.h"

/* defines -------------------------------------------------------------------*/
/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* variables (extern) --------------------------------------------------------*/
/* variables (global) --------------------------------------------------------*/
/* variables (local) ---------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* functions -----------------------------------------------------------------*/

/**
 * cls_init - initialize a class object
 * @c:         the class id
 * @mid:       module id
 * @obj_size:  the size of objects of the class
 * @obj_ops:   object operations
 *
 * return: 0 on success, negative value on error
 */
int cls_init(cls_id c, uint8_t mid, size_t obj_size, obj_ops_t *obj_ops)
{
    if (unlikely(c->inited)) {
        return 0;
    }

    c->mid = mid;
    c->obj_size = obj_size;
    c->obj_ops = obj_ops;

    dlist_init(&c->objs);

    BUG_ON(mutex_init(&c->lock) != 0, "Fail to init class!");

    c->inited = true;

    return 0;
}
